package com.codedog.utils;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.codedog.constant.UrlConstant;
import com.codedog.entity.UserInfo;
import com.codedog.enums.ResponseCodeEnum;
import com.codedog.exception.CustomException;

import java.util.HashMap;
import java.util.Map;

public class CodeDogHTTPUtil {
    public static Map<String, String> headerMap = new HashMap();
    public static Map<String, String> bodyMap = new HashMap<>();

    static {
        headerMap.put("Accept", "*/*");
        headerMap.put("Accept-Encoding", "gzip, deflate, br, zstd");
        headerMap.put("Accept-Language", "zh-CN,zh;q=0.9");
        headerMap.put("Connection", "keep-alive");
        headerMap.put("Content-Length", "21");
        headerMap.put("Content-Type", "application/json;charset=UTF-8");
        headerMap.put("Host", "cloud-gateway.codemao.cn");
        headerMap.put("Origin", "https://community-admin-codemaster.codemao.cn");
        headerMap.put("Referer", "https://community-admin-codemaster.codemao.cn/");
        headerMap.put("Sec-Fetch-Dest", "empty");
        headerMap.put("Sec-Fetch-Mode", "cors");
        headerMap.put("Sec-Fetch-Site", "same-site");
        headerMap.put("User-Agent", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Mobile Safari/537.36");
        headerMap.put("authorization_type", "3");
        headerMap.put("sec-ch-ua", "\"Chromium\";v=\"128\", \"Not;A=Brand\";v=\"24\", \"Google Chrome\";v=\"128\"");
        headerMap.put("sec-ch-ua-mobile", "?1");
        headerMap.put("sec-ch-ua-platform", "\"Android\"");

        bodyMap.put("page", "1");
        bodyMap.put("limit", "10");
    }

    public static UserInfo getUserInfo(String cookie) {
        HttpRequest get = createGet(UrlConstant.USER_INFO_URL, cookie);
        String body = get.execute().body();
        System.out.println(body);
        UserInfo userInfo = JSONUtil.toBean(body, UserInfo.class);
        return userInfo;
    }

    public static HttpRequest createGet(String url, String cookie) {
        HttpRequest get = HttpUtil.createGet(url);
        get.cookie(cookie);
        return get;
    }

    public static HttpRequest getPost() {
        HttpRequest post = HttpRequest.post(UrlConstant.DEFAULT_URL);
        for (Map.Entry<String, String> entry : headerMap.entrySet()) {
            post.header(entry.getKey(), entry.getValue());
        }
        post.body(JSONUtil.toJsonStr(bodyMap));
        return post;
    }

    public static HttpRequest getPost(String url) {
        HttpRequest post = HttpRequest.post(url);
        for (Map.Entry<String, String> entry : headerMap.entrySet()) {
            post.header(entry.getKey(), entry.getValue());
        }
        post.body(JSONUtil.toJsonStr(bodyMap));
        return post;
    }

    public static HttpRequest getPost(String url, String cookie) {
        HttpRequest post = HttpRequest.post(url);
        for (Map.Entry<String, String> entry : headerMap.entrySet()) {
            post.header(entry.getKey(), entry.getValue());
        }
        post.header("Cookie", cookie);
        post.body(JSONUtil.toJsonStr(bodyMap));
        return post;
    }

    public static HttpRequest getPostNotBody(String url, String cookie) {
        HttpRequest post = HttpRequest.post(url);
        for (Map.Entry<String, String> entry : headerMap.entrySet()) {
            post.header(entry.getKey(), entry.getValue());
        }
        post.header("Cookie", cookie);
        return post;
    }
}

